package cn.jellybmedia.org.controller;

import cn.jellybmedia.basic.utils.AjaxResult;
import cn.jellybmedia.basic.utils.QueryResult;
import cn.jellybmedia.org.domain.Department;
import cn.jellybmedia.org.query.DepartmentQuery;
import cn.jellybmedia.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门controller类
* @Author: Mr.Zhang
* @Version: 1.0
* @Date:  2022/6/23 19:46
*/
//打在Controller对Controller做描述
@Api(value = "部门的API",description="部门相关的CRUD功能")
// 复合注解,只会返回JSON数据,不会跳转页面
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    public IDepartmentService departmentService;

    /**
    * @Title: findDepartment
    * @Description: 根据id查询数据,使用的是Restful风格
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/23 20:31
    * @Parameters: [id]
    * @Return cn.jellybmedia.org.domain.Department
    */
    @ApiOperation(value = "通过ID查询" )
    // PathVariable:获取地址中的占位符.根据名称获取二级或者多级路径的参数
    // ApiParam:打在参数前，对参数做描述
    @GetMapping("/{id}")
    public Department findDepartment(@ApiParam(name="部门表主键Id",required = true)@PathVariable ("id") Long id){
        return  departmentService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询全部部门数据
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 9:58
    * @Parameters: []
    * @Return java.util.List<cn.jellybmedia.org.domain.Department>
    */
    @GetMapping
    public List<Department> loadAll(){
        return  departmentService.loadAll();
    }

    /**
    * @Title: findDepartment
    * @Description: 高级查询所有
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/23 20:47
    * @Parameters: [departmentQuery]
    * @Return java.util.List<cn.jellybmedia.org.domain.Department>
    */
    // RequestBody:
    // 每个方法都有自己的基本CRUD注解,默认不写的话,就会取占有基本的
    @PostMapping
    public QueryResult<Department> findDepartment(@RequestBody DepartmentQuery departmentQuery) {

        return departmentService.queryData(departmentQuery);
    }

    /**
    * @Title: delDepartment
    * @Description: 根据id进行删除
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/23 21:15
    * @Parameters: [id]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult delDepartment(@PathVariable ("id") Long id){
        try {
            // 执行成功,返回Ajaxresult中数据
            departmentService.remove(id);

            return  AjaxResult.getAjaxResult();

        }catch (Exception e){
            // 执行失败,进行返回,并返回提示信息
            e.printStackTrace();
            return  AjaxResult.error();
        }

    }
    
    /** 
    * @Title: addOrUpt
    * @Description: 新增与修改的方法
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/23 21:29  
    * @Parameters: [department]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() ==null){

                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return  AjaxResult.getAjaxResult();

        }catch (Exception e){
            // 执行失败,进行返回,并返回提示信息
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/26 8:41
    * @Parameters: [ids]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            // 执行成功,返回Ajaxresult中数据
            departmentService.batchDel(ids);
            return  AjaxResult.getAjaxResult();
        }catch (Exception e){
            // 执行失败,进行返回,并返回提示信息
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

    /**
    * @Title: getTree
    * @Description: 获取部门树
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/26 8:42
    * @Parameters: []
    * @Return java.util.List<cn.jellybmedia.org.domain.Department>
    */
    @GetMapping("/tree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }

}
